package response

import (
	"go.didapinche.com/foundation/plat-echo/app/ops/models"
	"go.didapinche.com/foundation/plat-echo/pkg/utils/data"
	"go.didapinche.com/time"
	"strings"
)

const (
	MAX     = "max"
	MIN     = "min"
	AVG     = "avg"
	TOTAL   = "total"
	CURRENT = "current"
)

type GraphType struct {
	Name          string   `json:"name,omitempty" form:"name"`
	Id            uint64   `json:"id,omitempty" form:"id"`
	Key           string   `json:"key,omitempty" form:"key"`
	Order         int      `json:"order,omitempty"`
	DefaultExpand bool     `json:"default_expand" form:"default_expand"`
	Types         []*Graph `json:"types,omitempty" yaml:"types"`
}

//模版和模版类型的连表查询
type Graph struct {
	ID            uint64                `gorm:"primary_key,column:id" json:"id"`
	Name          string                `gorm:"column:name" json:"name"`
	AppID         uint64                `gorm:"column:app_id" json:"app_id,omitempty" form:"app_id"`
	ApiID         uint64                `gorm:"column:api_id" json:"api_id,omitempty" form:"api_id"`
	HostID        uint64                `gorm:"column:host_id" json:"host_id,omitempty" form:"host_id"`
	DashboardID   uint64                `gorm:"column:dashboard_id" json:"dashboard_id" form:"dashboard_id"`
	TypeID        uint64                `gorm:"column:type_id" json:"type_id"` //模版分类ID
	Threshold     string                `json:"threshold" gorm:"-"`
	TypeName      string                `gorm:"column:type" json:"type_name"` //模版分类ID
	Kind          string                `gorm:"column:kind" json:"kind"`      //种类
	Format        models.UnitType       `gorm:"format" json:"format"`
	Decimals      int                   `gorm:"decimals" json:"decimals"`
	Tag           string                `gorm:"column:tag" json:"tag"`
	Order         int                   `gorm:"column:order" json:"order"`
	Order2        int                   `gorm:"column:order2" json:"order2"` //模版分类排序
	Queries       models.Queries        `gorm:"type:json;column:queries" json:"queries"`
	Visualization *models.Visualization `gorm:"type:json;column:visualization" json:"visualization"` // 可视化
	Alert         *models.Alert         `gorm:"type:json;column:alert" json:"alert,omitempty"`
	IsForce       bool                  `gorm:"column:is_force" json:"is_force"` // 强制订阅
	Subscription  *models.Subscription  `gorm:"type:json;column:subscription" json:"subscription"`
	Silence       *models.Silence       `gorm:"silence" json:"silence"`
	General       *models.General       `gorm:"general" json:"general"`
	CreateTime    time.Time             `gorm:"column:create_time" json:"create_time"`
	UpdateTime    time.Time             `gorm:"column:update_time" json:"update_time"`
	Key           string                `gorm:"column:key" json:"key"`
	Expand        bool                  `gorm:"column:expand" json:"default_expand" form:"default_expand"`
	Status        int                   `gorm:"-" json:"status"` //

}

func NewGraph(graph2 *models.GraphAndType) *Graph {
	graph := new(Graph)
	graph.Name = graph2.Name
	if graph2.Kind == 0 {
		graph.Kind = "app-template"
	} else if graph2.Kind == 1 {
		graph.Kind = "api-template"
	} else if graph2.Kind == 3 {
		graph.Kind = "dashboard"
	}
	if graph2.Type_Name == "custom" {
		graph.Kind = "custom"
	}
	graph.ID = graph2.ID
	graph.Status = graph2.Status
	graph.Visualization = graph2.Visualization
	graph.Alert = graph2.Alert
	graph.Queries = graph2.Queries
	graph.Silence = graph2.Silence
	graph.General = graph2.General
	graph.Key = graph2.Key
	graph.IsForce = graph2.IsForce
	graph.Expand = graph2.Expand
	graph.Order = graph2.Order
	graph.Order2 = graph2.Order2
	graph.TypeName = graph2.Type_Name
	graph.TypeID = graph2.TypeID
	graph.AppID = graph2.AppID
	graph.ApiID = graph2.ApiID
	graph.HostID = graph2.HostID
	graph.DashboardID = graph2.DashboardID
	subscription := new(models.Subscription)

	if graph2.Subscribe != nil {
		if graph2.IsOwner {
			for _, s := range graph2.Subscribe.SubscribeForce {
				if s == models.Sms {
					subscription.Sms = true
				}
				if s == models.Phone {
					subscription.Phone = true
				}
			}
		} else {
			if data.ContainString(graph2.Subscribe.Sms, graph2.UserID) {
				subscription.Sms = true
			}
			if data.ContainString(graph2.Subscribe.Phones, graph2.UserID) {
				subscription.Phone = true
			}
		}
	}
	graph.Subscription = subscription
	return graph
}

type GraphInfo struct {
	GraphType []*GraphType        `json:"graph" form:"graph"`
	App       *models.Application `json:"app" form:"app"`
	Api       *models.Api         `json:"api" form:"api"`
}

type MetricStat struct {
	Max     float64 `json:"max"`
	Min     float64 `json:"min"`
	Avg     float64 `json:"avg"`
	Total   float64 `json:"total"`
	Current float64 `json:"current"`
	Link    string  `json:"link,omitempty"`
}

//声明一个Hero结构体切片类型
type GraphTypeSlice []*GraphType

//切片实现Interface 接口的三个方法
//1.Len() ：返回切片的大小
func (m GraphTypeSlice) Len() int {
	return len(m)
}

//2.Less(i, j int) :决定使用什么规则进行排序
func (m GraphTypeSlice) Less(i, j int) bool {
	if strings.ToUpper(m[i].Name) == "DEFAULT" {
		return true
	}
	if strings.ToUpper(m[j].Name) == "DEFAULT" {
		return false
	}
	if m[i].Name == "Custom" {
		return false
	}
	if m[j].Name == "Custom" {
		return true
	}
	return m[i].Order < m[j].Order
}

//3.Swap(i, j int) :Less(i, j int)返回true时进行交换
func (m GraphTypeSlice) Swap(i, j int) {
	m[i], m[j] = m[j], m[i]
}

//声明一个Hero结构体切片类型
type GraphSlice []*Graph

//切片实现Interface 接口的三个方法
//1.Len() ：返回切片的大小
func (m GraphSlice) Len() int {
	return len(m)
}

//2.Less(i, j int) :决定使用什么规则进行排序
func (m GraphSlice) Less(i, j int) bool {
	return m[i].Order < m[j].Order
}

//3.Swap(i, j int) :Less(i, j int)返回true时进行交换
func (m GraphSlice) Swap(i, j int) {
	m[i], m[j] = m[j], m[i]
}

func (m *MetricStat) CalAvg(size int) float64 {
	if size == 0 || m.Total == 0 {
		m.Avg = 0
		return 0
	}
	m.Avg = m.Total / float64(size)
	return m.Avg
}
